package com.taobao.tbapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taobao.api.TaobaoResponse;

/**
 * exception when call taobao api
 * 
 * @author ly
 * @version
 * @Date 2013-8-17上午10:33:03
 * 
 */
public class TBApiBizException extends Exception {

  private final static Logger log            = LoggerFactory.getLogger(TBApiBizException.class);

  private long                userId;
  private TaobaoResponse      taobaoResponse = null;

  public TBApiBizException(Long userId, TaobaoResponse taobaoResponse) {
    super(taobaoResponse.getSubMsg());
    this.taobaoResponse = taobaoResponse;
  }

  public TBApiBizException(Long userId, String message, TaobaoResponse taobaoResponse) {
    super(message);
    this.taobaoResponse = taobaoResponse;
  }

  public TaobaoResponse getTaobaoRespnse() {
    return taobaoResponse;
  }

  public void setTaobaoRespnse(TaobaoResponse taobaoResponse) {
    this.taobaoResponse = taobaoResponse;
  }

  public static TBApiBizException genException(Long userId, TaobaoResponse rep) {
    // JDBCBase.insert(new TBApiBizExceptionLog(userId, rep));
    String message = "";
    if (rep == null) {
      message = "未知异常";
    } else {
      try {
        message = "Msg = " + rep.getMsg() + " SubMsg = " + rep.getSubMsg() + " ErrorCode = " + rep.getErrorCode() + " SubCode = " + rep.getSubCode();
      } catch (Exception e) {
        log.error("~~~~~~~~~~~~~~~");
      }
    }
    log.error(message);
    return new TBApiBizException(userId, message, rep);
  }

  public long getUserId() {
    return userId;
  }

  public void setUserId(long userId) {
    this.userId = userId;
  }
}
